package test

import config.{MyFile, MyJson, MyString}
import org.apache.commons.io.FileUtils

object SortJb extends MyFile with MyJson with MyString{

  def main(args: Array[String]): Unit = {

    num
  }

  def num = {
    val str = "I:\\muscle_db\\jb/refSeqs.json".readFileToString

    val lines = str.tail.init.split("},").init.map{x=>
      val line =  x.drop(2).replaceAll("\"","").split(",")
     // val l = line.filter(_.startsWith("des")).head.split(":").last
      val chr = line.filter(_.startsWith("name")).head.split(":").last
      (chr ,"" , x)
    }

    val chr = lines.filter(_._1.isDouble).sortBy{_._1.toInt}
    chr.foreach{x=>
      println(" + " +x._1)
    }
    lines.filter(!_._1.isDouble).foreach{x=>
      println(" + " +x._1)
    }

    val j = "[" +  (chr ++ lines.filter(!_._1.isDouble)).map(_._3).mkString("},") + "}]"
    FileUtils.writeStringToFile("I:\\muscle_db\\jb/Homo/refSeqs.json".toFile,j)

  }

  def chr = {
    val str = "H:\\牛蒡基因组数据库\\jb".readFileToString

    val lines = str.tail.init.split("},").init.map{x=>
      val line =  x.drop(2).replaceAll("\"","").split(",")
      val l = line.filter(_.startsWith("des")).head.split(":").last
      val chr = line.filter(_.startsWith("name")).head.split(":").last
      (chr ,l , x)
    }

    val chr = lines.filter(_._1.toLowerCase.startsWith("chr")).sortBy{x=>
      x._2.toUpperCase match{
        case "X" => 100
        case "Y" => 101
        case "MT" => 102
        case "M" => 103
        case i => i.replaceAll("CHR","").toInt
      }
    }
    val j = "[" +  (chr ++ lines.filter(!_._1.toLowerCase.startsWith("chr"))).map(_._3).mkString("},") + "}]"
    FileUtils.writeStringToFile("I:\\muscle_db\\jb/Homo/refSeqs.json".toFile,j)

  }

}
